package net.homeip.jcperera.webinfo.beans;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;

import net.homeip.jcperera.nu.core.boot.Boot;
import net.homeip.jcperera.nu.core.msg.BaseMessage;
import net.homeip.jcperera.nu.core.msg.MessageBagRequest;
import net.homeip.jcperera.webinfo.WebInfoSite;

import org.primefaces.event.SelectEvent;

@ManagedBean
@ViewScoped
public class WebInfoSiteBean implements Serializable
{
    private static final long serialVersionUID = 7925375251653288475L;

    private WebInfoSite       webInfoSite;

    public WebInfoSiteBean()
    {
        webInfoSite = new WebInfoSite();
    }

    public List<WebInfoSite> getAllWebInfoSites()
    {
        EntityManagerFactory emf = Boot.getSystem().getPersistence().getEntityManagerFactory();
        EntityManager entityManager = (EntityManager) emf.createEntityManager();
        TypedQuery<WebInfoSite> wisQuery = entityManager.createQuery("SELECT OBJECT(wis) FROM WebInfoSite wis",
                WebInfoSite.class);
        List<WebInfoSite> allWebInfoSites = wisQuery.getResultList();
        entityManager.close();
        return allWebInfoSites;
    }

    public void saveWebInfoSite(ActionEvent actionEvent)
    {
        saveWebInfoSite();
    }

    public String saveWebInfoSite()
    {
        if (webInfoSite != null)
        {
            EntityManagerFactory emf = Boot.getSystem().getPersistence().getEntityManagerFactory();
            EntityManager entityManager = (EntityManager) emf.createEntityManager();
            entityManager.getTransaction().begin();
            if (webInfoSite.getEntityid() != null && webInfoSite.getEntityid().length() > 0)
                webInfoSite = entityManager.merge(webInfoSite);
            entityManager.persist(webInfoSite);
            entityManager.getTransaction().commit();
            entityManager.getEntityManagerFactory().getCache().evictAll();
            entityManager.close();
            MessageBagRequest messageBagRequest = new MessageBagRequest();
            messageBagRequest.addInformationalMessage("nu.core.entity.saved.ok", BaseMessage.FRIENDLINESS_FRIENDLY);
            messageBagRequest.addAllMessagesToFacesContext(FacesContext.getCurrentInstance());
        }
        return "edit.xhtml?entityid=" + this.getEntityid();
    }

    public void deleteWebInfoSite(ActionEvent actionEvent)
    {
        deleteWebInfoSite();
    }

    public String deleteWebInfoSite()
    {
        if (webInfoSite != null)
        {
            MessageBagRequest messageBagRequest = new MessageBagRequest();
            String entityid = webInfoSite.getEntityid();
            if (entityid != null && entityid.trim().length() > 0)
            {
                EntityManagerFactory emf = Boot.getSystem().getPersistence().getEntityManagerFactory();
                EntityManager entityManager = (EntityManager) emf.createEntityManager();
                entityManager.getTransaction().begin();
                webInfoSite = entityManager.merge(webInfoSite);
                entityManager.remove(webInfoSite);
                messageBagRequest.addInformationalMessage("nu.core.entity.deleted.ok",
                        BaseMessage.FRIENDLINESS_FRIENDLY);
                entityManager.getTransaction().commit();
                entityManager.getEntityManagerFactory().getCache().evictAll();
                entityManager.close();
            }
            messageBagRequest.addAllMessagesToFacesContext(FacesContext.getCurrentInstance());
        }
        return "/pages/webinfosite/list.xhtml?faces-redirect=true";
    }

    public String getEntityid()
    {
        return webInfoSite.getEntityid();
    }

    public void setEntityid(String entityid)
    {
        MessageBagRequest messageBagRequest = new MessageBagRequest();
        if (entityid != null)
        {
            EntityManagerFactory emf = Boot.getSystem().getPersistence().getEntityManagerFactory();
            EntityManager entityManager = (EntityManager) emf.createEntityManager();
            entityManager.getTransaction().begin();
            WebInfoSite webInfoSite = null;
            TypedQuery<WebInfoSite> wisQuery = entityManager.createQuery(
                    "SELECT OBJECT(wis) FROM WebInfoSite wis WHERE wis.entityid = :entityid", WebInfoSite.class);
            wisQuery.setParameter("entityid", entityid);
            webInfoSite = wisQuery.getSingleResult();
            if (webInfoSite == null)
            {
                Hashtable<String, String> extensions = new Hashtable<String, String>();
                extensions.put("id", entityid);
                extensions.put("description", "Entity not found on database");
                messageBagRequest.addErrorMessage("nu.core.entity.type.load.error", BaseMessage.FRIENDLINESS_FRIENDLY,
                        extensions);
            }
            else this.webInfoSite = webInfoSite;
            entityManager.getTransaction().commit();
            entityManager.close();
        }
        messageBagRequest.addAllMessagesToFacesContext(FacesContext.getCurrentInstance());
    }

    public void onRowSelect(SelectEvent event)
    {
        try
        {
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("edit.xhtml?entityid=" + webInfoSite.getEntityid());
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public String getUrl()
    {
        return webInfoSite.getUrl();
    }

    public void setUrl(String url)
    {
        webInfoSite.setUrl(url);
    }

    public String getParameters()
    {
        return webInfoSite.getParameters();
    }

    public void setParameters(String parameters)
    {
        webInfoSite.setParameters(parameters);
    }

    public String getCreatedBy()
    {
        return webInfoSite.getCreatedBy();
    }

    public Date getCreatedWhen()
    {
        return webInfoSite.getCreatedWhen().getTime();
    }

    public String getModifiedBy()
    {
        return webInfoSite.getModifiedBy();
    }

    public Date getMofifiedWhen()
    {
        return webInfoSite.getModifiedWhen().getTime();
    }

    public long getLocalVersion()
    {
        return webInfoSite.getLocalVersion();
    }

    public long getGlobalVersion()
    {
        return webInfoSite.getGlobalVersion();
    }

    public void setSelectedWebInfoSite(WebInfoSite webInfoSite)
    {
        this.webInfoSite = webInfoSite;
    }

    public WebInfoSite getSelectedWebInfoSite()
    {
        return this.webInfoSite;
    }

    public WebInfoSite getWebInfoSite()
    {
        return webInfoSite;
    }
}
